package com.hmsm.server.util;

import java.util.Scanner;

public class NumUtil {

	/**
	 *  @描述:货币转换大写功能
	 *  @方法名称:numtochinese
	 *  @作者:fengtao
	 *  @创建日期:2014-4-11 下午04:51:51 
	 *  @param value
	 *  @return
	 **/
	@Deprecated
	public static String numtochinese(double value) {
		return toUpperRMB(value);
	}

	/**
	 * @param args
	 */
	public static String numtochinese(String numString) {
		String s1 = "零壹贰叁肆伍陆柒捌玖";
		String s4 = "分角整元拾佰仟万拾佰仟亿拾佰仟";
		String temp = "";
		String result = "";
		Scanner s = new Scanner(numString);
		String string = String.valueOf(s.nextDouble());
		s.close();
		if (string == null)
			return "输入字串不是数字串只能包括以下字符（'0'～'9'，'.')，输入字串最大只能精确到仟亿，小数点只能两位！";
		temp = string.trim();
		try {

			Float.parseFloat(temp);

		} catch (Exception e) {
			return "输入字串不是数字串只能包括以下字符（'0'～'9'，'.')，输入字串最大只能精确到仟亿，小数点只能两位！";
		}
		int len = 0;
		if (temp.indexOf(".") == -1)
			len = temp.length();
		else
			len = temp.indexOf(".");
		if (len > s4.length() - 3)
			return ("输入字串最大只能精确到仟亿，小数点只能两位！");
		int n1 = 0;
		String num = "";
		String unit = "";

		for (int i = 0; i < temp.length(); i++) {
			if (i > len + 2) {
				break;
			}
			if (i == len) {
				continue;
			}
			n1 = Integer.parseInt(String.valueOf(temp.charAt(i)));
			num = s1.substring(n1, n1 + 1);
			n1 = len - i + 2;
			unit = s4.substring(n1, n1 + 1);
			result = result.concat(num).concat(unit);
		}
		if ((len == temp.length()) || (len == temp.length() - 1))
			result = result.concat("整");
		if (len == temp.length() - 2)
			result = result.concat("零分");
		result = result.substring(0, result.length() - 5);
		return result;
	}

	/**
	 * 
	 *  @描述:人民币小写转换成大写汉字(最完整的转换方法)
	 *  @方法名称 :toUpperRMB
	 *  @作者     :yaosq
	 *  @创建日期 :2014-5-23 上午11:01:38 
	 *  @param rMBNum
	 *  @return changeResult
	 *
	 */
	public static String toUpperRMB(double rMBNum){  
	  	  
	        char[] s1 = {'零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'};     
	        char[] s4 = {'分', '角', '圆', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '万'};          
	        String str = String.valueOf(Math.round(rMBNum * 100 + 0.00001));          
	        String changeResult = "";         
	        for (int i = 0; i <str.length(); i++) {      
	      	  int n = str.charAt(str.length() - 1 - i) - '0';      
	      	    changeResult = s1[n] + "" + s4[i] + changeResult;          
	      	  }             
	        changeResult = changeResult.replaceAll("零仟", "零");          
	        changeResult = changeResult.replaceAll("零佰", "零");          
	        changeResult = changeResult.replaceAll("零拾", "零");          
	        changeResult = changeResult.replaceAll("零亿", "亿");          
	        changeResult = changeResult.replaceAll("零万", "万");          
	        changeResult = changeResult.replaceAll("零元", "元");          
	        changeResult = changeResult.replaceAll("零角", "零");          
	        changeResult = changeResult.replaceAll("零分", "零");             
	        changeResult = changeResult.replaceAll("零零", "零");          
	        changeResult = changeResult.replaceAll("零亿", "亿");          
	        changeResult = changeResult.replaceAll("零零", "零");          
	        changeResult = changeResult.replaceAll("零万", "万");          
	        changeResult = changeResult.replaceAll("零零", "零");          
	        changeResult = changeResult.replaceAll("零元", "元");          
	        changeResult = changeResult.replaceAll("亿万","亿");           
	        changeResult = changeResult.replaceAll("零$", "");          
	        changeResult = changeResult.replaceAll("圆$", "圆整");          
	        changeResult = changeResult.replaceAll("角$", "角整");         
	        return changeResult;   
        }

	public static void main(String[] args) {
		System.out.println(NumUtil.numtochinese(1234567899.126));
		System.out.println(NumUtil.toUpperRMB(1234567899.126));
	}

}
